//
//  WBHomeViewController.swift
//  迷你微博
//
//  Created by Json on 2020/2/3.
//  Copyright © 2020 Json. All rights reserved.
//

import UIKit

//定义全局常量，尽量用private修饰，否则所有地方都能访问
private let cellId = "cellId"

class WBHomeViewController: WBBaseViewController {

    //微博数据数组
    private lazy var statusList = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    //加载数据
    override func loadData() {
        
        
        /** 测试加载数据方法都封装到extension中
                 let urlString = "https://api.weibo.com/2/statuses/public_timeline.json"
                 let params = ["access_token" : "2.00klD_FI0Nfwca4080d6ce2fFVaLHE"]
                 
         //        WBNetworkManager.shared.get(urlString, parameters: params, progress: nil, success: { (_, json) in
         //            print(json)
         //        }) { (_, error) in
         //            print("网络请求失败 \(error)")
         //        }
                 
                 WBNetworkManager.shared.request(URLString: urlString, parameters: params) { (json, isSuccess) in
                     print(json)
                 }
         */
        
        // 测试加载微博数据
        print("开始加载数据 \(WBNetworkManager.shared)")
        
        WBNetworkManager.shared.statusList { (list, isSuccess) in
            print(list)
        }
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
           
            print("插入数据")
            //将数据插入到数组顶部
            for i in 0..<15 {
                if self.isPullup {
                    //将数据追加到底部
                    self.statusList.append("上拉 \(i)")
                } else {
                    self.statusList.insert(i.description, at: 0)
                }
            }
            
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            //恢复上拉刷新控件
            self.isPullup = false
            print("刷新表格")
            //刷新表格
            self.tableView?.reloadData()
        }
    }
    
    //显示好友
    @objc private func showFriends(){
        print(#function)
        let vc = WBDemoViewController()
        //push时隐藏导航栏
        //vc.hidesBottomBarWhenPushed = true
        //push的动作是nav..做的
        navigationController?.pushViewController(vc, animated: true)
    }
}

// 表格数据源方法,具体数据源方法实现
extension WBHomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 1.取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        // 2.设置cell
        cell.textLabel?.text = statusList[indexPath.row]
        // 3.返回cell
        return cell
    }
}

//设置界面
extension WBHomeViewController{
    
    //重写父类方法
    override func setupTableView() {
        super.setupTableView()
        
        //设置导航栏按钮，没有高亮
       // navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
        
        //高亮导航按钮
//        let btn: UIButton = UIButton.cz_textButton("好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
//        btn.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
//        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        
        //抽取高亮按钮
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
    
        //注册原型cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
}
